﻿using System;
using System.Configuration;
using System.Linq;

namespace Facebook.Configuration
{
    [ConfigurationCollection(typeof(FacebookApplicationElement))]
    public class FacebookApplicationElementCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FacebookApplicationElement();
        }

        protected override Object GetElementKey(ConfigurationElement element)
        {
            var app = (FacebookApplicationElement)element;
            return app.Environment + app.ApiKey;
        }

        public FacebookApplicationElement GetDefault()
        {
            String _environment;
            _environment = ConfigurationManager.AppSettings["Environment"] ?? String.Empty;

            return (
                from app in this.OfType<FacebookApplicationElement>()
                orderby app.IsDefault descending
                where (_environment == app.Environment)
                select app).FirstOrDefault();
        }
    }
}